﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using BusinessCommon.Utilities;
using CustomControlLibrary;
using StoreWinform.Controller;
using System.IO;

namespace StoreWinform
{
    /// <summary>
    /// Login form
    /// </summary>
    public partial class LoginForm : Form
    {
        LoginFormController control;
        StmProcessBar stmProcessBar = StmProcessBar.Instance;

        public LoginForm()
        {
            InitializeComponent();
            SetLanguage();
            InitForm();
        }

        /// <summary>
        /// InitForm
        /// </summary>
        private void InitForm()
        {
            HideMessage();
        }

        /// <summary>
        /// Do login
        /// </summary>
        private void DoLogin()
        {
            bool success = false;

            txtUserName.Text = txtUserName.Text.Trim();
            string UserName = txtUserName.Text;
            string Password = txtPassword.Text;

            Thread backgroundThread = new Thread(
                new ThreadStart(() =>
                {
                    // Login action
                    if (control.UserLogin(UserName, Password))
                    {
                        success = true;
                    }
                    stmProcessBar.Close();
                    //// Invoke close process bar
                    //if (stmProcessBar.InvokeRequired)
                    //{
                    //    stmProcessBar.BeginInvoke(new Action(() => stmProcessBar.Close()));
                    //}
                }
            ));
            backgroundThread.Start();
            backgroundThread.Join();
            if (success)
            {
                this.Visible = false;
                MenuController.Instance.GetView.Visible = true;
            }
        }

        /// <summary>
        /// Do clear
        /// </summary>
        private void DoClear()
        {
            txtUserName.Text = CommonString.BLANK;
            txtPassword.Text = CommonString.BLANK;
            HideMessage();
            txtUserName.Focus();
        }

        /// <summary>
        /// Hide message box
        /// </summary>
        private void HideMessage()
        {
            splitContainer.Panel1Collapsed = true;
            splitContainer.Panel1.Hide();
        }

        /// <summary>
        /// Set language
        /// </summary>
        private void SetLanguage()
        {
            btnLogin.setMultiLanguage(this.Name, btnLogin.Name);
            btnClear.setMultiLanguage(this.Name, btnClear.Name);
            lblPassword.setMultiLanguage(this.Name, lblPassword.Name);
            lblUserName.setMultiLanguage(this.Name, lblUserName.Name);
            this.Text = ResxManagerString.GetPropertyString(this.Name, this.Name);
        }

        /// <summary>
        /// Validate required
        /// </summary>
        private void validateRequired()
        {
            if (!String.IsNullOrEmpty(txtUserName.Text)
                && !String.IsNullOrEmpty(txtPassword.Text))
            {
                btnLogin.Enabled = true;
            }
            else
            {
                btnLogin.Enabled = false;
            }
        }

        /// <summary>
        /// Show message box
        /// </summary>
        /// <param name="errCode">errCode</param>
        /// <param name="listBoxType">Msg box type</param>
        public void ShowMessage(string errCode, SmEnum.MSG_BOX_TYPE listBoxType)
        {
            List<string> listMessageErr = new List<string>();

            if (!String.IsNullOrEmpty(errCode))
            {
                string messageErr = "";
                string[] paramError = new string[5];
                if (errCode.Equals(ItemDefineIF.ERR_00001))
                {
                    paramError[0] = lblUserName.Text;
                    paramError[1] = lblPassword.Text;
                    messageErr = ErrorProcessor.CreateMessageError(errCode, paramError);
                }
                else
                {
                    paramError = new string[5];
                    messageErr = ErrorProcessor.CreateMessageError(errCode, paramError);
                }
                listMessageErr.Add(messageErr);
            }
            splitContainer.Panel1Collapsed = false;
            splitContainer.Panel1.Show();
            smMessageListBox.ShowMessage(listMessageErr, listBoxType);
        }

        public void ShowChangePasswordForm()
        {
            this.Visible = false;
            ChangePassFormController controller = new ChangePassFormController();
            controller.OpenChangePassForm();
        }

        public void SetController(LoginFormController loginController)
        {
            control = loginController;
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            validateRequired();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            validateRequired();
        }

        private void btnCloseMsgBox_Click(object sender, EventArgs e)
        {
            HideMessage();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            DoLogin();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            DoClear();
        }
    }
}
